package cate.game.pvp.guildwar;

import cate.common.table.d.GDFight;
import cate.common.table.d.GDObj;
import cate.common.table.d.GDOperation;
import cate.common.table.pvp.guildwar.row.GuildWarPtRow;
import cate.game.play.proce.play.PlayPO;
import cate.game.res.MixRes;
import cate.game.res.MixResAdder;
import cate.game.res.MixResItem;
import cate.game.role.Role;
import cate.game.role.farm.AbstractFarmFTC;
import cate.game.role.pvp.guildwar.RoleGuildWar;
import cate.game.role.pvp.guildwar.msg.GuildWarChallengeResp;

public class GuildWarFTC extends AbstractFarmFTC<RoleGuildWar> {

    private int difficulty;

    private final GuildWarPtRow ptRow;

    public GuildWarFTC(Role role,int difficulty, GuildWarPtRow ptRow) {
        setup(role);
        this.difficulty = difficulty;
        this.ptRow = ptRow;
    }

    @Override
    protected RoleGuildWar po(Role role) {
        return role.getPvp().guildWar;
    }

    @Override
    public void handleResult(Role role, PlayPO playPO) {
        super.handleResult(role, playPO);
        boolean win = playPO.winner == GDFight.Team.A;
        MixRes rwd = new MixRes();
        long point;
        if (win) {
            point = ptRow.getPoint(difficulty);
            rwd.addList(new MixRes(ptRow.winRwdStr));
        } else {
            point = ptRow.failPoint;
            rwd.addList(new MixRes(ptRow.loseRwdStr));
        }
        role.getPvp().guildWar.addPoint(point);
        rwd.addItem(new MixResItem(GDObj.Type.NUM, GDObj.Num.GUILD_POINT, point));
        // 记录排行榜
        role.getGame().pvp.guildWar.getMessagingModule().recordRoleRank(role);
        GuildWarChallengeResp resp = new GuildWarChallengeResp(win, rwd);
        new MixResAdder().setRes(rwd).setOperation(GDOperation.GUILD_WAR_CHALLENGE)
                .setOperationSub(difficulty).exe(role);
        resp.send(role);
        po(role).noticeUpdate();
    }
}
